package com.example.blackbox.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.blackbox.Data.News;
import com.example.blackbox.R;

import java.util.ArrayList;

public class OtherNewsAdapter extends BaseAdapter {
    ArrayList<News> news=new ArrayList<>();

    public OtherNewsAdapter(ArrayList<News> news) {
        this.news = news;
    }

    @Override
    public int getCount() {
        return news.size();
    }

    @Override
    public Object getItem(int position) {
        return news.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,null);
            viewHolder=new ViewHolder();
            viewHolder.newsTitle=convertView.findViewById(R.id.news_title);
            viewHolder.newsTag=convertView.findViewById(R.id.news_tag);
            viewHolder.newsDate=convertView.findViewById(R.id.news_date);
            viewHolder.newsMessage=convertView.findViewById(R.id.news_message);
            viewHolder.newsImg=convertView.findViewById(R.id.news_img);

            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        News data=news.get(position);
        viewHolder.newsTitle.setText(data.getNewsTitle());
        viewHolder.newsTag.setText(data.getNewsTag());
        viewHolder.newsDate.setText(data.getNewsDate());
        viewHolder.newsMessage.setText(data.getNewsMessage());

        Glide.with(convertView).load(data.getImgUrl()).into(viewHolder.newsImg);

        return convertView;
    }

    class ViewHolder{
        TextView newsTitle,newsTag,newsMessage,newsDate;
        ImageView newsImg;
    }
}
